var express = require('express');
var router = express.Router();
var api = require('../api');
/* GET home page. */
var obj = {};
router.get('/', function  (req, res, next) {
	  api.getlogin(0).then(data=>{
	  	if(data.status=='success'){
	  		api.findblog().then(reas=>{
			  	if(reas.status=='success'){
			  		obj.person = data.data;
	  				console.log(obj.person);

			  		obj.article = reas.data;
			  		obj.types = 'home';
			  		res.render('index', obj);
			  	}
			  })
	  	    
	  	}
	  })
});
//博客详情
router.get('/articledetails', function  (req, res, next) {
	var params = req.query||res.param;
	var blogid = params.blogid;
	if(typeof blogid==undefined||!obj.person){
		res.redirect('/')
	}
	  api.getblogDetails(blogid).then(data=>{
	  	if(data.status=='success'){
	  		obj.articledetails = data.data;
	  		obj.types = 'homedetails';
	  	    res.render('index',obj );
	  	}else{
	  		res.redirect('/')
	  	}
	  })
});
//我的相册
router.get('/share', function  (req, res, next) {

	if(!obj.person){
		res.redirect('/')
	}
	obj.types = 'photos';
	res.render('index',obj );
	  
});
//我的日记列表
router.get('/list', function  (req, res, next) {
	  api.getlogin(0).then(data=>{
	  	if(data.status=='success'){
	  		api.findblog(0).then(reas=>{
	  			console.log(reas);
			  	if(reas.status=='success'){
			  		obj.person = data.data;
			  		obj.article = reas.data;
			  		obj.types = 'list';
			  		res.render('index', obj);
			  	}else{
			  		//处理博客没有数据的时候
			  		obj.person = data.data;
			  		obj.article = [];
			  		obj.types = 'list';
			  		res.render('index', obj);
			  	}
			  })
	  	    
	  	}
	  })
});
router.get('/gbook', function  (req, res, next) {

	  api.getlogin(0).then(data=>{
	  	if(data.status=='success'){
	  		api.findblog(0).then(reas=>{
			  	if(reas.status=='success'){
			  		obj.person = data.data;
			  		obj.article = reas.data;
			  		obj.types = 'gbook';
			  		res.render('index', obj);
			  	}
			  })
	  	    
	  	}
	  })
});
module.exports = router;
